###############################################################################
# Configuration

INCLUDE_PATH =	"/opt/rezome/inc"

###############################################################################

import telnetlib
import sys
import MySQLdb
sys.path.append(INCLUDE_PATH)
from switchs import *
from mysql import *

def get_switch_list():
	db = MySQLdb.connect(host=MYSQL_HOST, user=MYSQL_USER, passwd=MYSQL_PASSWD, db=MYSQL_DB)
	c = db.cursor()
	c.execute("SELECT ipv4_switch FROM switch WHERE actif_switch='1' AND reference='2950';")
	rows = c.fetchall()
	list = []
	for ip in rows:
		list.append(ip[0])
	return list

def checked_read(tn, waited_string):
	#print "Waiting: " + waited_string
	received = tn.read_until(waited_string, 40)
	#print "Got: " + received
	if received.endswith(waited_string):
		return received
	else:
		print "ERROR: received '" + received + "' while waiting '" + waited_string + "'"
		return False

def connect(ip):
	tn = telnetlib.Telnet(ip)
	#tn.set_debuglevel(1000)
	if not checked_read(tn, "Password:"):
		return None
	tn.write(SWITCHS_PASSWORD + "\n")
	if not checked_read(tn, ">"):
		return None
	return tn

def enable(tn):
	tn.write("enable\n")
	if not checked_read(tn, "Password:"):
		return None
	tn.write(SWITCHS_PASSWORD + "\n")
	data = checked_read(tn, "#")
	if not data:
		return None
	return data.split("\n")[-1][0:-1]

def dump_config(tn, filename):
	tn.write("copy running-config tftp://" + SWITCHS_TFTP + "/dump/%(filename)s\n" % locals() )
	if not checked_read(tn, "?"):
		return None
	tn.write("\n")
	if not checked_read(tn, "?"):
		return None
	tn.write("\n")
	infos = checked_read(tn, "#")
	return infos

def load_config(tn, filename):
	tn.write("copy tftp://" + SWITCHS_TFTP + "/%(filename)s running-config\n" % locals() )
	if not checked_read(tn, "?"):
		return None
	tn.write("\n")
	infos = checked_read(tn, "#")
	return infos

def copy_running_to_startup_config(tn):
	tn.write("copy running-config startup-config\n")
	if not checked_read(tn, "?"):
		return None
	tn.write("\n")
	infos = checked_read(tn, "#")
	return infos

def get_ios_version(tn):
	tn.write("show version\n")
	data = tn.read_until(" --More-- ")
	lines = data.split("\n")
	tn.write(" ")
	data = tn.read_until("#")
	return lines[2]

def quit(tn):
	tn.write("quit\n")
	tn.read_all()

def set_acl(tn, vlan):
	if int(vlan)==100:
		tn.write("ip access-group 110 in\n")
		if not checked_read(tn, "#"):
			return None
	elif int(vlan)==200:
		tn.write("ip access-group 120 in\n")
		if not checked_read(tn, "#"):
			return None
	elif int(vlan)==250:
		tn.write("ip access-group 125 in\n")
		if not checked_read(tn, "#"):
			return None
	else:
		return

def change_vlan(tn, port, vlan):
	tn.write("conf t\n")
	if not checked_read(tn, "#"):
		return None
	tn.write("interface FastEthernet0/" + port + "\n")
	if not checked_read(tn, "#"):
		return None

	# reset acl
	tn.write("no ip access-group in\n")
	if not checked_read(tn, "#"):
		return None
	tn.write("no ip access-group out\n")
	if not checked_read(tn, "#"):
		return None
	tn.write("no switchport access vlan\n")
	if not checked_read(tn, "#"):
		return None

	if int(vlan) != 0:
		# Dans ce cas on n'a pas de trunk
		tn.write("no switchport mode trunk\n")
		if not checked_read(tn, "#"):
			return None
		tn.write("switchport mode access\n")
		if not checked_read(tn, "#"):
			return None
		tn.write("switchport access vlan " + vlan + "\n")
		if not checked_read(tn, "#"):
			return None
		set_acl(tn, vlan)
	else:
		tn.write("no switchport mode access\n")
		if not checked_read(tn, "#"):
			return None
		tn.write("switchport mode trunk\n")
		if not checked_read(tn, "#"):
			return None

	tn.write("exit\n")
	if not checked_read(tn, "#"):
		return None
	tn.write("exit\n")
	if not checked_read(tn, "#"):
		return None
	return True

def run_command(tn, command):
	tn.write(command + "\n")
	index = 1
	infos = ""
	while index == 1:
		(index, matchobject, text) = tn.expect( ["#", " --More-- "], 5)
		infos = infos + text
		if index == 1:
			tn.write(" ")
	return infos

def create_vlan(tn, number, name):
	tn.write("conf t\n")
	if not checked_read(tn, "#"):
		return None
	tn.write("vlan "+number+"\n")
	if not checked_read(tn, "#"):
		return None
	tn.write("name "+name+"\n")
	if not checked_read(tn, "#"):
		return None
	tn.write("no shutdown\n")
	if not checked_read(tn, "#"):
		return None
	tn.write("state active\n")
	if not checked_read(tn, "#"):
		return None
	tn.write("exit\n")
	if not checked_read(tn, "#"):
		return None
	tn.write("exit\n")
	if not checked_read(tn, "#"):
		return None
	return True

